//
//  KWMRequestListResult.m
//  iLiJiPao
//
//  Created by kevin on 4/27/15.
//  Copyright (c) 2015 kollway. All rights reserved.
//

#import "KWMRequestListResult.h"

@implementation KWMRequestListResult{

}

- (instancetype)initWithDictionary:(NSDictionary *)dict
                        modelClass:(Class)modelClass
                             error:(NSError **)err {
    self = [super initWithDictionary:dict error:err];
    if (self) {
        NSArray *dataArray = dict[@"data"];
        if(dataArray){
            self.data = [self buildListData:modelClass jsonDictionary:dataArray];
        }
    }
    
    return self;
}

- (NSArray *)buildListData:(Class)modelClass jsonDictionary:(NSArray *)dataArray {
    if(modelClass == nil){
        NSString *reason = [[NSString alloc] initWithFormat:@"class with name:%@ doesn't exists!!", NSStringFromClass(modelClass)];
        NSAssert(NO, reason);
    }
    if(! [dataArray isKindOfClass:[NSArray class]]){
        NSString *reason = [[NSString alloc] initWithFormat:@"dataArray object is not a NSArray instance"];
        NSAssert(NO, reason);
    }
    NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
    for(NSDictionary *itemJson in dataArray){
        NSError *error;
        id result = [[modelClass alloc] initWithDictionary:itemJson error:&error];
        if(error){
            NSLog(@"buildListData error: %@", [error localizedDescription]);
        }else{
            [mutableArray addObject:result];
        }
    }
    return [[NSArray alloc] initWithArray:mutableArray];
}

+ (JSONKeyMapper *)keyMapper {
    return [[JSONKeyMapper alloc] initWithDictionary:@{
                                                       @"code" : @"code",
                                                       @"message" : @"message",
                                                       @"page_size" : @"pageSize",
                                                       @"total_count" : @"totalCount",
                                                       @"page" : @"page"
                                                       }];
}


@end
